package com.nulldev.util.gui.fontAPI;

import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.VariableAPI.util.random.GlobalRandom;
import com.nulldev.util.data.Arrays.maps.FastMaps;

public class FontManager {
	private final static Map<String, Font> registeredFonts = FastMaps.map();
	private final static String[] PREF_FONTS = new String[]
		{ "Prelude Medium", "PreludeWGL Medium", "Prelude", "BreezeSans", "Product Sans", "Roboto", "Arial", "Noto Sans", "Liberation Sans" };

	static {
		if (registeredFonts.isEmpty()) {
			final String[] families = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
			for (final String family : families) {
				final Font f = new Font(family);
				registeredFonts.put(family, f);
			}
		}
	}

	public static void __call_static() {
	}

	public static void reloadFonts() {
		registeredFonts.clear();
		__call_static();
	}

	public static void main(String[] args) {
		__call_static();
	}

	public static Map<String, Font> getFontMap() {
		return Collections.unmodifiableMap(registeredFonts);
	}

	public static Font font(final String name) {
		return registeredFonts.get(name);
	}

	public static java.awt.Font getFontSetAsAWT() {
		return gets(PREF_FONTS).getFont(16);
	}

	public static java.awt.Font getFontSetAWT() {
		return gets(PREF_FONTS).getFont();
	}

	public static java.awt.Font getFontSetAsAWT(final int size) {
		return gets(PREF_FONTS).getFont(size);
	}

	public static Font getFontSet() {
		return gets(PREF_FONTS);
	}

	public static Font random() {
		return ArrayUtils.random(registeredFonts);
	}

	public static Font gets(final String... fonts) {
		for (final String s : fonts) {
			if (registeredFonts.containsKey(s))
				return registeredFonts.get(s);
		}
		return monospaced();
	}

	public static Font randGets(final String... fonts) {
		final List<String> fontList = Arrays.asList(fonts);
		Collections.shuffle(fontList, GlobalRandom.getGlobalInsecureRandom());
		for (final String s : fontList) {
			if (registeredFonts.containsKey(s))
				return registeredFonts.get(s);
		}
		return monospaced();
	}

	public static Font monospaced() {
		return new Font(java.awt.Font.MONOSPACED);
	}
}
